package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsTargetCategory;
import com.sg.service.base.app.MOmsTargetCategoryService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsTargetCategoryMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTargetCategoryServiceImpl
        extends ServiceImpl<OmsTargetCategoryMapper, OmsTargetCategory>
        implements MOmsTargetCategoryService {

    @Autowired
    OmsTargetCategoryMapper omsTargetCategoryMapper;

    /**
     * 3-3-02删除目标分类[2941]
     * gen by moon at 10/15/2022, 10:02:34 PM
     */
    @Trace(operationName = "3-3-02删除目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTargetCat(String targetCategoryId) {

        return super.removeById(targetCategoryId);
    }

    /**
     * 3-3-02修改目标分类[2685]
     * gen by moon at 10/15/2022, 10:02:33 PM
     */
    @Trace(operationName = "3-3-02修改目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetCat(OmsTargetCategory omsTargetCategory) {
        omsTargetCategory.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategory.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsTargetCategory);
    }

    /**
     * 3-3-02查目标分类详情[2595]
     * gen by moon at 3/30/2023, 12:39:07 PM
     */
    @Trace(operationName = "3-3-02查目标分类详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetCategory queryTargetCatDetail(QueryTargetCatDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetCategory> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetCategory retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02查目标分类列表[2592]
     * gen by moon at 10/15/2022, 10:02:30 PM
     */
    @Trace(operationName = "3-3-02查目标分类列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategory> queryTargetCatList(QueryTargetCatListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsTargetCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02新增目标分类[2591]
     * gen by moon at 5/17/2024, 11:08:36 PM
     */
    @Trace(operationName = "3-3-02新增目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTargetCat(OmsTargetCategory omsTargetCategory) {
        omsTargetCategory.setTargetCategoryId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetCategory.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsTargetCategory.setIsUpdate("TRUE");
        omsTargetCategory.setUpdateStatus("NEW");
        omsTargetCategory.setArchivngType("NOT_ARCHIVE");
        omsTargetCategory.setIsEditing("TRUE");
        omsTargetCategory.setIsArchive("FALSE");
        omsTargetCategory.setSubjectLifeCycle("EDITING");
        omsTargetCategory.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategory.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategory.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategory.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategory.setIsValid("FALSE");
        omsTargetCategory.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetCategory.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetCategory)) {
            return omsTargetCategory.getTargetCategoryId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-3-02批量查目标分类列表[2254]
     * gen by moon at 10/15/2022, 10:02:24 PM
     */
    @Trace(operationName = "3-3-02批量查目标分类列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategory> batchQueryTargetCat(BatchQueryTargetCatReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            if (reqDto.getSetConstructionTaskList() == null || reqDto.getSetConstructionTaskList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("target_category_id", reqDto.getSetConstructionTaskList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-3-02批量改目标分类[3643]
     * gen by moon at 10/15/2022, 10:02:36 PM
     */
    @Trace(operationName = "3-3-02批量改目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTargetCat(List<OmsTargetCategory> omsTargetCategory) {

        if (omsTargetCategory == null || omsTargetCategory.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsTargetCategory);

    }

    /**
     * 3-3-02批量删目标分类[3644]
     * gen by moon at 10/15/2022, 10:02:37 PM
     */
    @Trace(operationName = "3-3-02批量删目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTargetCat(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M3-3-02构造目标分类验证数据[4244]
     * gen by moon at 10/15/2022, 10:02:38 PM
     */
    @Trace(operationName = "M3-3-02构造目标分类验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String buildSingleTargetCategory(OmsTargetCategory omsTargetCategory) {
        omsTargetCategory.setTargetCategoryId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetCategory.setTargetCategoryNumber(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsTargetCategory.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetCategory.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsTargetCategory != null && omsTargetCategory.getSubjectLifeCycle() != null && omsTargetCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetCategory.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsTargetCategory != null && omsTargetCategory.getSubjectLifeCycle() != null && omsTargetCategory.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetCategory.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsTargetCategory.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetCategory.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTargetCategory != null && omsTargetCategory.getSubjectLifeCycle() != null && omsTargetCategory.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetCategory.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsTargetCategory != null && omsTargetCategory.getSubjectLifeCycle() != null && omsTargetCategory.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetCategory.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsTargetCategory.setIsValid("FALSE");
        omsTargetCategory.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsTargetCategory.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetCategory)) {
            return omsTargetCategory.getTargetCategoryId();
        } else {
            return "-1";
        }

    }

    /**
     * M3-查询单条目标分类详情[4328]
     * gen by moon at 3/30/2023, 12:39:08 PM
     */
    @Trace(operationName = " M3-查询单条目标分类详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetCategory querySingleTargetCategoryDetail(QuerySingleTargetCategoryDetailReq reqDto) {

        try {

            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetCategory> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetCategory retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M-3查询多条目标分类列表[4364]
     * gen by moon at 10/15/2022, 10:02:39 PM
     */
    @Trace(operationName = "M-3查询多条目标分类列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategory> queryMultiTargetCatList(OmsTargetCategory omsTargetCategory) {

        try {
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsTargetCategory);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-批量删多条目标分类[4365]
     * gen by moon at 10/15/2022, 10:02:40 PM
     */
    @Trace(operationName = "M3-批量删多条目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteMultiTargetCat(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M3-查询目标分类兄弟列表[4375]
     * gen by moon at 10/15/2022, 10:02:43 PM
     */
    @Trace(operationName = "M3-查询目标分类兄弟列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategory> queryTargetCatBrotherList(QueryTargetCatBrotherListReq reqDto) {

        try {
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-3-02删除单条目标分类[4511]
     * gen by moon at 10/15/2022, 10:02:48 PM
     */
    @Trace(operationName = "M3-3-02删除单条目标分类")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSingleTargetCategory(String targetCategoryId) {

        return super.removeById(targetCategoryId);
    }

    /**
     * M3查询多条目标分类测试列表[6259]
     * gen by moon at 4/21/2023, 9:50:28 PM
     */
    @Trace(operationName = "M3查询多条目标分类测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetCategory> queryMultiTargetCatTestDataList(OmsTargetCategory omsTargetCategory) {

        try {
            QueryWrapper<OmsTargetCategory> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsTargetCategory);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetCategory.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetCategory.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetCategory>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
